<?php 
class Scan_history extends CI_Controller {
	
	function scan_history()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('encrypt');
		$this->load->library('fpdf');
		$this->load->library('enc');
                $this->load->model('mmaster');
                $this->load->model('mglobal');
				$this->load->model('mscan_history');
		$this->is_logged_in();
		#$this->mjadwal->saveLog();
		$this->load->helper('url');
		$this->load->helper('file');
	}
	
	function is_logged_in()
	{
		$is_logged_in = $this->session->userdata('is_logged_in');
		if(!isset($is_logged_in) || $is_logged_in != true)
		{
			redirect(base_url() .'index.php/login');
		}		
	}
	
	//function index($act="",$page="")
	function index($lastId= "")
	{
		$data['lastId'] = $lastId;
		$data['template'] 	= "shell/smooth";
		$data['main_view']  = 'f-scan-history';
		$this->load->view($data['template'],$data);
	}

	
		
    // untuk manage data yang ditampilkan pada grid view
    public function read() {
		//echo json_encode( $this->mscan_history->getAllScanin() );
		
		//edited by Alfin Akhret
		if($this->session->userdata('is_self_service') == 1)
		{
			$result = $this->mscan_history->getAllScaninCurrentUser();

			for($i=0;$i<count($result['aaData']);$i++)
       {
    			// $result[$i]->scan_in_id = $this->enc->fogit($result[$i]->scan_in_id);
    			$result['aaData'][$i][2] = $this->enc->fogit($result['aaData'][$i][2]); //edited by: alfin akhret
    	 }

		}
		else
		{
			$result = $this->mscan_history->getAllScanin();
			//encode
       for($i=0;$i<count($result['aaData']);$i++)
       {
    			// $result[$i]->scan_in_id = $this->enc->fogit($result[$i]->scan_in_id);
    			$result['aaData'][$i][2] = $this->enc->fogit($result['aaData'][$i][2]); //edited by: alfin akhret
    	 }
		}

    		 //penambahan nomor statis untuk grid SSP
        $start=$_GET['iDisplayStart'];
        $result['iDisplayStart']=$start;
        
        echo json_encode($result);
		
	}
	
	function defoger()
    {
        $result = $this->enc->defog($this->input->post('str'));
        echo $result;  
    }

	//concate id pada row table tertentu untuk keperluan view
	public function concateId($id)
	{
		$tempId = "" . $id;
		if (strlen($tempId) < 8)
		{
			for($ii=strlen($tempId); $ii<8; $ii++)
			{
				$tempId = '0' . $tempId;
			}
		}
		return $tempId;
	}
	
	//proses data untuk halaman detail
	function getDetail($id1='', $id2='')
	{
		//$scan_id = $id1;
		//id yang masuk di encode
		$scan_id = $this->enc->defog($id1);  
		
		$temp = $this->mscan_history->getById($scan_id);
		$sales_id = $temp->user_id;
		
		// $temp = "";
		// $temp = $this->mscan->getClusterName($this->session->userdata('username'));
		// $data['ter_name'] 	= $temp[0]['territory_name'];
		// $ter_id 			= $temp[0]['territory_id'];
		
		
		$temp = $this->mscan_history->getClusterName($this->session->userdata('username'));	
		if (count($temp) > 0)
			foreach($temp as $result)
			{
				$data['ter_name'] = $result['territory_name'];
				$ter_id = $result['territory_id'];
			}
		else
		{
			$data['ter_name'] = "No Data";
			$ter_id = "No Data";
		}
		$list = $this->pdfAndDetail($scan_id, $ter_id);
			$data['strId'] = $list[0];
			$data['scan_in'] = $list[2];
			$data['sales_name'] = $list[3];
			$data['scan_in_detail'] = $list[4];

		$data['record'] = $this->mscan_history->getIccidByScanId($scan_id);

		$data['template'] 	= "shell/smooth";
		$data['main_view']  = 'f-scan-history-detail';
		$this->load->view($data['template'],$data);
			
	}	

	function tet($scan_id) {
		$test = $this->mscan_history->getPdfDetails($scan_id);
		foreach ($test as $key) {
			echo $key->item_name;
		}
	}
	
	//proses data untuk men-generate file pdf dan detail view
	function pdfAndDetail($scan_id, $ter_id)
	{
		$list = array();
		
		$list[0] = $this->concateId($scan_id);
		
		$list[2] = $this->mscan_history->getById($scan_id);
		
		$this->db->select('user_name');
		$this->db->where('user_id', $list[2]->user_id);
		$q = $this->db->get('t_mtr_user');
		$list[3] = $q->result_array();		
		
		$this->db->select('*');
		$this->db->where('scan_in_id', $scan_id);
		$q = $this->db->get('t_trx_scan_in_detail');
		$list[4] = $q->result_array();
		
		for($ii=0;$ii<count($list[4]);$ii++)
		{
			$list[$ii+5] = $this->mscan_history->getPdfDetails($scan_id);
		}
		
		return $list;
	
	}

	//proses data untuk men-generate file pdf
	function topdf($id1='', $id2='')
	{
	
		//$scan_id = $id1;
		
		//id yang masuk di encode
		$scan_id = $this->enc->defog($id1);
		 
		$temp = $this->mscan_history->getById($scan_id);
		$sales_id = $temp->user_id;		
		
		$temp = $this->mscan_history->getClusterName($this->session->userdata('username'));	
		if (count($temp) > 0)
			foreach($temp as $result)
			{
				$ter_name = $result['territory_name'];
				$ter_id  = $result['territory_id'];
			}
		else
		{
			$ter_name = "No Data";
			$ter_id  = "No Data";
		}
		

		$list 		= $this->pdfAndDetail($scan_id, $ter_id);
		$strId 		= $list[0];
		$scan_in 	= $list[2];
		$sales_name = $list[3];
		// $scan_in_detail = $list[4];
		$scan_in_detail = $this->mscan_history->getPdfDetails($scan_id);
		
		// for($ii=0;$ii<count($list[4]);$ii++)	
		// {
		// 	$itemDetail[$ii] = $list[5+$ii];
		// }
		
		//begin configuration
		$textColour = array( 0, 0, 0 );
		$headerColour = array( 100, 100, 100 );
		$tableHeaderTopTextColour = array( 0, 0, 0 );
		$tableHeaderTopFillColour = array( 250, 250, 250 );
		$tableHeaderTopProductTextColour = array( 0, 0, 0 );
		$tableHeaderTopProductFillColour = array( 143, 173, 204 );
		$tableHeaderLeftTextColour = array( 99, 42, 57 );
		$tableHeaderLeftFillColour = array( 184, 207, 229 );
		$tableBorderColour = array( 50, 50, 50 );
		$tableRowFillColour = array( 255, 255, 255 );
		

		$chartColours = array(
						  array( 255, 100, 100 ),
						  array( 100, 255, 100 ),
						  array( 100, 100, 255 ),
						  array( 255, 255, 100 ),
						);

		$data = array(
				  array( 9940, 10100, 9490, 11730 ),
				  array( 19310, 21140, 20560, 22590 ),
				  array( 25110, 26260, 25210, 28370 ),
				  array( 27650, 24550, 30040, 31980 ),
				);
		// End configuration
		
		
		$this->load->helper('path');

		$font_directory = './fpdf_fonts/';
		set_realpath($font_directory);
		define('FPDF_FONTPATH',$font_directory);
		
		$this->fpdf->Open();
		$this->fpdf->AddPage();
		$this->fpdf->SetAutoPageBreak(true, 5);
		$this->fpdf->SetMargins(10, 20, 20);
		$this->fpdf->AliasNbPages();
		
		
		//header
		//$this->fpdf->Image(BASEPATH.'../file/images/logo_smartfren.png',10,3,50,0,'');	
		$this->fpdf->Image(FCPATH.'file/images/logo_smartfren.png',10,3,50,0,'');			
		$this->fpdf->SetFont('Arial','B',12);
		$this->fpdf->Cell(180,-5,"Delivery Notes",0,1,'R');	
		$this->fpdf->Ln( 10 );
		$this->fpdf->SetFont('Arial','B',8);
		$this->fpdf->Cell(190,0,"(Pengambilan Barang dari Cluster)",0,1,'R');		
		$this->fpdf->Line(10, 20, 200, 20);
		
		//table
		$this->fpdf->SetDrawColor( $tableBorderColour[0], $tableBorderColour[1], $tableBorderColour[2] );
		
		//Cluster Information		
		$this->fpdf->Ln(5);
		$this->fpdf->SetFont('Arial','B',16);		
		$this->fpdf->Write(19, $ter_name);
		$this->fpdf->Ln( 6 );
		
		
		// Date and receipt no
		$this->fpdf->Ln(15);
		$this->fpdf->SetXY(130, 42);
		$this->fpdf->SetFont('Arial','B',8);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->Cell( 30, 8, "Tanggal", 1, 0, 'C', true );
		$this->fpdf->Cell( 40, 8, "No. Scan In", 1, 0, 'C', true );		
		$this->fpdf->SetXY(130, 50);
		
		$fill = false;
		$row = 0;		
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->SetFont( 'Arial', '', 8 );		
		$this->fpdf->Cell( 30, 8, date("d-m-Y", strtotime($scan_in->scan_date)).' '.$scan_in->data_timestamp, 1, 0, 'C', $fill );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );		
		$this->fpdf->Cell( 40, 8, $strId, 1, 0, 'C', $fill );
					
	
		$this->fpdf->Ln( 20 );
			
		// Order Information
		$this->fpdf->SetXY(10, 42);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 25, 8, "Sales ID", 1, 0, 'C', true );
		$this->fpdf->Cell( 45, 8, "Nama Sales", 1, 0, 'C', true );		
		$this->fpdf->Ln( 8 );
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 25, 8, $scan_in->user_id, 1, 0, 'C', $fill );
		$this->fpdf->Cell( 45, 8, $sales_name[0]['user_name'], 1, 0, 'C', $fill );		

		
		// Order Information
		$this->fpdf->Ln( 20 );
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 30, 8, "Kode Barang", 1, 0, 'C', true );
		$this->fpdf->Cell( 15, 8, "Jumlah", 1, 0, 'C', true );		
		$this->fpdf->Cell( 70, 8, "Nama Barang", 1, 0, 'C', true );		
		$this->fpdf->Cell( 25, 8, "Item Group", 1, 0, 'C', true );		
		$this->fpdf->Cell( 25, 8, "Harga (Rp.)", 1, 0, 'C', true );			
		$this->fpdf->Cell( 25, 8, "Subtotal (Rp.)", 1, 0, 'C', true );			
		$this->fpdf->Ln( 1 );
		
		$price = 0;
		$subtotal = 0;
		$subname = "";

		$this->fpdf->SetFont( 'Arial', '', 7 );
		$this->fpdf->Ln( 7 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );

		foreach ($scan_in_detail as $detail) {
			$jumlah = $detail->jumlah;
			$harga = $detail->harga;

			$this->fpdf->Cell( 30, 7, $detail->item_code, 1, 0, 'L', $fill );
			$this->fpdf->Cell( 15, 7, $detail->jumlah, 1, 0, 'C', $fill );
			$this->fpdf->Cell( 70, 7, $detail->item_name, 1, 0, 'L', $fill );
			$this->fpdf->Cell( 25, 7, $detail->item_group_name, 1, 0, 'L', $fill );
			$this->fpdf->Cell( 25, 7, number_format($harga, 2, "," , "."), 1, 0, 'R', $fill );
			$this->fpdf->Cell( 25, 7, number_format($harga * $jumlah, 2, "," , "."), 1, 0, 'R', $fill );
			$this->fpdf->Ln( 7 );
			$subtotal += ($harga*$jumlah);
		}

		// subtotal
		$this->fpdf->SetX(150);
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 25, 8, "Subtotal :", 1, 0, 'C', true );
		
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 25, 8, number_format($subtotal, 2, "," , "."), 1, 0, 'R', $fill );

		if ($this->fpdf->GetY() >= 267) {
			$this->fpdf->SetY(-15); 
			$this->fpdf->Cell(0, 8, "~~ Printed by " . $this->session->userdata('username') . " ~~", 0, 0, 'C' );
			//Arial italic 8 

			$this->fpdf->SetFont('Arial','I',8); 
			$this->fpdf->Cell(10,8,'Page '.$this->fpdf->PageNo().'/{nb}',0,0,'R'); 
	
		}
		
		//$this->fpdf->Cell(160,-5,"Delivery Notes",0,1,'R');	
		

		
		//Signature of Person Handling Over
		//Signature of Recipient
		
		// get nik name from sales and current user login
		$userID = $scan_in->user_id;
		$recipient = $this->mscan_history->getNikName($userID);
		$handler = $this->mscan_history->getNikName($this->session->userdata('username'));
		
		// signature field
		$this->fpdf->Ln( 30 );
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 55, 12, "Signature of Person Handling Over:", 1, 0, 'L', true );
		
		 
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 60, 12, $handler, 1, 0, 'L', $fill );
		$this->fpdf->Cell( 40, 12, "", 1, 0, 'L', $fill );
		
		$this->fpdf->Ln( 12 );
		$this->fpdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] );
		$this->fpdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );
		$this->fpdf->SetFont( 'Arial', 'B', 8 );
		$this->fpdf->Cell( 55, 12, "Signature of Recipient:", 1, 0, 'L', true );	
		
		$this->fpdf->SetFont( 'Arial', '', 8 );
		$this->fpdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] );
		$this->fpdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowFillColour[2] );
		$this->fpdf->Cell( 60, 12, $recipient, 1, 0, 'L', $fill );
		$this->fpdf->Cell( 40, 12, "", 1, 0, 'L', $fill );
		
		
		//Position at 1.5 cm from bottom 
		$this->fpdf->SetY(-15); 
		$this->fpdf->Cell(0, 8, "~~ Printed by " . $this->session->userdata('username') . " ~~", 0, 0, 'C' );
		$this->fpdf->SetFont('Arial','I',8); 
		//Page number 
		$this->fpdf->Cell(10,8,'Page '.$this->fpdf->PageNo().'/{nb}',0,0,'R'); 
		$this->fpdf->Output();
	
	}
	
}